/**
 * 1004. 最大连续1的个数 III
 * https://leetcode.cn/problems/max-consecutive-ones-iii/description/
 */
class Solution2 {
    public int longestOnes(int[] nums, int k) {
        int n = nums.length, ret = 0, zero = 0;
        for(int left = 0, right = 0; right < n; right++) {
            // 1、进窗口
            if(nums[right] == 0) zero++;
            // 2、判断
            while(zero > k) {
                if(nums[left] == 0) zero--;
                left++;
            }
            // 3、更新结果
            ret = Math.max(ret, right - left + 1);
        }
        return ret;
    }
}